package com.xd.source.base;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavHostController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import com.xd.navi.NetWorkManager;
import com.xd.navi.OnNetStateChangeListener;
import com.xd.source.R;
import com.xd.source.utils.AppHelper;
import com.xd.source.utils.SysUtil;

import java.util.List;

import static com.xd.navi.NetWorkManager.*;


public abstract  class BaseActivity extends AppCompatActivity implements OnNetStateChangeListener {

    public NavController mNavController;
    public abstract  int getLayout();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        //状态兰相关
        SysUtil.setRootViewFitsSystemWindows(this,false);
        SysUtil.setTranslucentStatus(this);
        //navigation相关
        mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
        //注册相关网络信息
        getLifecycle().addObserver(NetWorkManager.getInstance());
        NetWorkManager.getInstance().addListener(this);
    }


    @Override
    public void onNetStateChange(boolean isConnect) {
        //tOdO()网络状态情况
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NetWorkManager.getInstance().removeListener(this);
    }

    /**
     * 检查Fragment是否在其容器中处于最顶端
     *
     * @param clazz 类
     */
    public boolean checkFragmentIsOnContainerTop(Class<?> clazz) {
        // 获取HomeFragment的NavHostFragment
        Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        return loopFragmentAndChildFragments(navHostFragment, clazz);
    }

    /**
     * 递归遍历可见栈
     */
    private boolean loopFragmentAndChildFragments(Fragment fragment, Class<?> clazz) {
        List<Fragment> fragments = fragment.getChildFragmentManager().getFragments();
        if (AppHelper.isEmpty(fragments)) {
            return checkFragmentResult(fragment, clazz);
        }
        for (Fragment childFragment : fragments) { // 遍历栈内Fragment集合
            if (checkFragmentResult(childFragment, clazz)) {
                return true;
            }
            if (AppHelper.noEmpty(fragment.getChildFragmentManager().getFragments())) {
                if (loopFragmentAndChildFragments(childFragment, clazz)) {
                    return true;
                }
            }
        }
        return false; // 这里表示遍历完毕仍未找到返回false
    }

    /**
     * @param clazz 想要获取的fragment
     * @return 如果当前fragment在显示 返回fragment实体，否则返回null
     * 说明不在栈顶显示
     */
    public Fragment getVisibleChildFragment(Class<?> clazz) {
        // 获取HomeFragment的NavHostFragment
        Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        return getFragmentFromInnerChild(navHostFragment, clazz);
    }

    /**
     * 递归遍历可见栈
     */
    private Fragment getFragmentFromInnerChild(Fragment fragment, Class<?> clazz) {
        List<Fragment> fragments = fragment.getChildFragmentManager().getFragments();
        if (AppHelper.isEmpty(fragments)) {
            if (checkFragmentResult(fragment, clazz)) {
                return fragment;
            }
        }
        for (Fragment childFragment : fragments) { // 遍历栈内Fragment集合
            if (checkFragmentResult(childFragment, clazz)) {
                return childFragment;
            }
            if (AppHelper.noEmpty(fragment.getChildFragmentManager().getFragments())) {
                Fragment fragment1 = getFragmentFromInnerChild(childFragment, clazz);
                if (fragment1 != null) {
                    return fragment1;
                }
            }
        }
        return null;
    }

    /**
     * 检查匹配结果
     */
    private boolean checkFragmentResult(Fragment fragment, Class<?> clazz) {
        if (fragment != null) {
            return clazz.isAssignableFrom(fragment.getClass()) && fragment.isVisible();
        }
        return false;
    }
}
